﻿using DreamSpace.Net;
using DreamSpace.Net.Utilities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DreamSpace.Net
{
    class Program
    {
        static void Main(string[] args)
        {
            try
            {
                Console.Title = "DreamSpace.NET Server Console";

                Console.Write(Environment.NewLine + "Starting a default ServerHost instance ...");

                var dsServer = new DreamSpaceServer();

                var mainHost = dsServer.CreateV8ServerHost();
                mainHost.Start();

                Console.WriteLine(Environment.NewLine + "... Done!");
                Console.WriteLine(Environment.NewLine + "Ready - just enter script to execute. Type '\\help' for a list of console specific commands.");

                string input, lcInput;

                while (true)
                {
                    Console.Write(Environment.NewLine + "> ");

                    input = Console.ReadLine();
                    lcInput = input.Trim().ToLower();

                    if (lcInput == @"\help" || lcInput == @"\")
                    {
                        Console.WriteLine(@"Special console commands (all commands are triggered via a preceding '\' character so as not to confuse it with script code):");
                        Console.WriteLine(@"\cls - Clears the screen.");
                        Console.WriteLine(@"\exit - Exists the console.");
                    }
                    else if (lcInput == @"\cls")
                        Console.Clear();
                    else if (lcInput == @"\exit")
                    {
                        Console.WriteLine("User aborted.");
                        break;
                    }
                    else
                    {
                        Console.WriteLine("");

                        var result = mainHost.ExecuteCommand(input);

                        Console.WriteLine(result);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine();
                Console.WriteLine();
                Console.WriteLine(Exceptions.GetFullErrorMessage(ex));
                Console.WriteLine();
                Console.WriteLine("Error!  Press any key to exit ...");
                Console.ReadKey();
            }
        }
    }
}
